﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MapInfo.MiPro.Interop;
using System.Windows.Forms;
using XtremeDockingPane;
using AxXtremeDockingPane;
using System.Drawing;
using MapWindowDocking.Mapinfo;
using System.Diagnostics;
using System.Globalization;
using System.Threading;


namespace MapWindowDocking
{
	/// <summary>
	/// 
	/// </summary>
	/// <remarks>
	/// This class should not be renamed unless the code in the mbx is also fixed to match the name of the class.
	/// </remarks>
    public class Class1
    {
        /// <summary>
        /// If the window focus has changed in the Mapinfo application.
        /// </summary>
        public static bool WindowFocusHasChanged { get; set; }

        private static List<WindowDockManger> DockMangerList = new List<WindowDockManger>();

        public static MapinfoWrapper mapinfosession;

    	/// <summary>
        /// Entry point for the MapWindowDockingLoader.MBX
    	/// </summary>
    	/// <remarks>This method should not be renamed unless
    	/// the code in the mbx is also fixed to match the name of the method.</remarks>
        public static void LoadDock()
        {
            if (mapinfosession == null)
            {
                mapinfosession = MapinfoWrapper.GetMapinfoServiceFromInterop();
            }

        	Window frontwindow = mapinfosession.GetFrontWindow();
        	
        	// If the front window is not a map window just show a message and exit the entry routine.
        	if (!mapinfosession.WindowIsMapWindow(frontwindow.ID))
        	{
        		MessageBox.Show("Selected window is not a map window \n This tool can only be used with a map window",
        		                "Error", 
        		                MessageBoxButtons.OK,
        		                MessageBoxIcon.Error);
        	}

            // Query the list of already used dock managers so that we don't have to recreate, due to a problem with the
            // third party docking manager creating a new docking window for a window that had one will thorw a AccessViolation Error.
            WindowDockManger docker = Class1.DockMangerList.FirstOrDefault(win => win.MapWindow.ID == frontwindow.ID);
            if (docker == null)
            {
                docker = new WindowDockManger(mapinfosession, frontwindow);
            }

            docker.CreateLegendPanel();
            //docker.CreateWindowInfoPanel();

            Class1.DockMangerList.Add(docker);
        }

        /// <summary>
        /// Unloads the docking manger from the supplied window if it has one.
        /// </summary>
        /// <param name="WindowID"></param>
        public static void UnLoadDock(int WindowID)
        {
            if (mapinfosession != null)
            {
                WindowDockManger manger = Class1.DockMangerList.FirstOrDefault(win => win.MapWindow.ID == WindowID);
                if (manger != null)
                {
                    manger.Unload();
                }
            }
        }

        /// <summary>
        /// Called when the focus between windows changes in Mapinfo.
        /// </summary>
        /// <param name="windowid"></param>
        public static void WindowFocusChanged(int windowid)
        {
            Class1.WindowFocusHasChanged = true;
        }

        /// <summary>
        /// Called when a map window in Mapinfo is closed.
        /// </summary>
        /// <param name="windowID"></param>
        public static void MapWindowClosed(int windowID)
        {
            if (mapinfosession != null)
            {
                WindowDockManger manger = Class1.DockMangerList.FirstOrDefault(win => win.MapWindow.ID == windowID);
                if (manger != null)
                {
                    manger.Unload();
                    Class1.DockMangerList.Remove(manger);
                    manger = null;
                }
            }
        }
    }  
}
